package bs_services

import (
	"fmt"
	"log"
	"strings"
	"xiehui-service/app/models/bs_models"
	"xiehui-service/global"

	"go.uber.org/zap"
)

type produceService struct {
}

var ProduceColNames = bs_models.Produce{}.GenerateColumnNames()
var ProduceColsStr = strings.Join(ProduceColNames, ", ")

var ProduceService = new(produceService)

// 查询派车单
func (produceService *produceService) ProduceList(stamp float64, num int64) (produces []bs_models.Produce, err error) {
	err = global.App.MSACCESS.Ping()
	if err != nil {
		log.Println("ProduceList ping==>>", err)
		global.App.Log.Error("查询派车单数据失败：", zap.Any("数据库联接", "ping失败"))
		return nil, err
	}
	sql := fmt.Sprintf("select Top %d %s from Produce where Stamp > %.10f order by DatTim asc", num, ProduceColsStr, stamp)

	rows, err := global.App.MSACCESS.Query(sql)
	if err != nil {
		log.Println("ProduceList query==>>", err)
		global.App.Log.Error("查询派车单数据失败：", zap.Any("Query查询失败", err))
		return nil, err
	}
	defer rows.Close()

	for rows.Next() {
		var produceItem bs_models.Produce
		var scanFields = []interface{}{
			&produceItem.ID,
			&produceItem.Code,
			&produceItem.DatTim,
			&produceItem.Attribute,
			&produceItem.Contract,
			&produceItem.Customer,
			&produceItem.ProjName,
			&produceItem.ProjType,
			&produceItem.ProjGrade,
			&produceItem.ProjArea,
			&produceItem.ProjAdr,
			&produceItem.Distance,
			&produceItem.ConsPos,
			&produceItem.Pour,
			&produceItem.Variety,
			&produceItem.BetLev,
			&produceItem.Filter,
			&produceItem.Freeze,
			&produceItem.Lands,
			&produceItem.Cement,
			&produceItem.Stone,
			&produceItem.BnSize,
			&produceItem.AddLiq,
			&produceItem.Request,
			&produceItem.Recipe,
			&produceItem.MixLast,
			&produceItem.MorRec,
			&produceItem.Mete,
			&produceItem.BegTim,
			&produceItem.EndTim,
			&produceItem.Attamper,
			&produceItem.Data,
			&produceItem.Flag,
			&produceItem.Notes,
			&produceItem.Vehicle,
			&produceItem.Driver,
			&produceItem.ProdTimB,
			&produceItem.ProdTimE,
			&produceItem.ProdMete,
			&produceItem.MorMete,
			&produceItem.ProdErr,
			&produceItem.ProdCnt,
			&produceItem.MorCnt,
			&produceItem.TotVehs,
			&produceItem.TotMete,
			&produceItem.Qualitor,
			&produceItem.Operator,
			&produceItem.LeftTim,
			&produceItem.ArriveTim,
			&produceItem.ChkLands,
			&produceItem.ChkTemp,
			&produceItem.UnloadTim,
			&produceItem.OverTim,
			&produceItem.Acceptor,
			&produceItem.Mark,
			&produceItem.MISID,
			&produceItem.Stamp,
			&produceItem.Task,
			&produceItem.Contacts,
			&produceItem.ContTel,
			&produceItem.Bend,
		}
		if err := rows.Scan(scanFields...); err != nil {
			log.Println("ProduceList scan==>>", err)
		}
		produces = append(produces, produceItem)
	}

	if err := rows.Err(); err != nil {
		log.Println("ProduceList rows.err==>>", err)
		global.App.Log.Error("查询派车单数据失败：", zap.Any("查询失败", err))
		return nil, err
	}

	return produces, nil
}

func (produceService *produceService) GetBetLvById(id string) (betLev string, err error) {
	err = global.App.MSACCESS.Ping()
	if err != nil {
		log.Println("GetBetLvById ping==>>", err)
		global.App.Log.Error("查询砼品种数据失败：", zap.Any("数据库联接", "ping失败"))
		return "", err
	}

	sql := fmt.Sprintf("select %s from Produce where ID = ?  order by DatTim asc", ProduceColsStr)
	row := global.App.MSACCESS.QueryRow(sql, id)

	var produceItem bs_models.Produce
	var scanFields = []interface{}{
		&produceItem.ID,
		&produceItem.Code,
		&produceItem.DatTim,
		&produceItem.Attribute,
		&produceItem.Contract,
		&produceItem.Customer,
		&produceItem.ProjName,
		&produceItem.ProjType,
		&produceItem.ProjGrade,
		&produceItem.ProjArea,
		&produceItem.ProjAdr,
		&produceItem.Distance,
		&produceItem.ConsPos,
		&produceItem.Pour,
		&produceItem.Variety,
		&produceItem.BetLev,
		&produceItem.Filter,
		&produceItem.Freeze,
		&produceItem.Lands,
		&produceItem.Cement,
		&produceItem.Stone,
		&produceItem.BnSize,
		&produceItem.AddLiq,
		&produceItem.Request,
		&produceItem.Recipe,
		&produceItem.MixLast,
		&produceItem.MorRec,
		&produceItem.Mete,
		&produceItem.BegTim,
		&produceItem.EndTim,
		&produceItem.Attamper,
		&produceItem.Data,
		&produceItem.Flag,
		&produceItem.Notes,
		&produceItem.Vehicle,
		&produceItem.Driver,
		&produceItem.ProdTimB,
		&produceItem.ProdTimE,
		&produceItem.ProdMete,
		&produceItem.MorMete,
		&produceItem.ProdErr,
		&produceItem.ProdCnt,
		&produceItem.MorCnt,
		&produceItem.TotVehs,
		&produceItem.TotMete,
		&produceItem.Qualitor,
		&produceItem.Operator,
		&produceItem.LeftTim,
		&produceItem.ArriveTim,
		&produceItem.ChkLands,
		&produceItem.ChkTemp,
		&produceItem.UnloadTim,
		&produceItem.OverTim,
		&produceItem.Acceptor,
		&produceItem.Mark,
		&produceItem.MISID,
		&produceItem.Stamp,
		&produceItem.Task,
		&produceItem.Contacts,
		&produceItem.ContTel,
		&produceItem.Bend,
	}
	if err := row.Scan(scanFields...); err != nil {
		log.Println("GetBetLvById scan==>>", err)
		return "", err
	}

	betLev = produceItem.BetLev
	return betLev, nil
}

func (produceService *produceService) GetOneById(id string) (produce *bs_models.Produce, err error) {
	err = global.App.MSACCESS.Ping()
	if err != nil {
		log.Println("GetOneById ping==>>", err)
		global.App.Log.Error("查询派车单数据失败：", zap.Any("数据库联接", "ping失败"))
		return nil, err
	}

	sql := fmt.Sprintf("select %s from Produce where ID = ?  order by DatTim asc", ProduceColsStr)
	row := global.App.MSACCESS.QueryRow(sql, id)

	var produceItem bs_models.Produce
	var scanFields = []interface{}{
		&produceItem.ID,
		&produceItem.Code,
		&produceItem.DatTim,
		&produceItem.Attribute,
		&produceItem.Contract,
		&produceItem.Customer,
		&produceItem.ProjName,
		&produceItem.ProjType,
		&produceItem.ProjGrade,
		&produceItem.ProjArea,
		&produceItem.ProjAdr,
		&produceItem.Distance,
		&produceItem.ConsPos,
		&produceItem.Pour,
		&produceItem.Variety,
		&produceItem.BetLev,
		&produceItem.Filter,
		&produceItem.Freeze,
		&produceItem.Lands,
		&produceItem.Cement,
		&produceItem.Stone,
		&produceItem.BnSize,
		&produceItem.AddLiq,
		&produceItem.Request,
		&produceItem.Recipe,
		&produceItem.MixLast,
		&produceItem.MorRec,
		&produceItem.Mete,
		&produceItem.BegTim,
		&produceItem.EndTim,
		&produceItem.Attamper,
		&produceItem.Data,
		&produceItem.Flag,
		&produceItem.Notes,
		&produceItem.Vehicle,
		&produceItem.Driver,
		&produceItem.ProdTimB,
		&produceItem.ProdTimE,
		&produceItem.ProdMete,
		&produceItem.MorMete,
		&produceItem.ProdErr,
		&produceItem.ProdCnt,
		&produceItem.MorCnt,
		&produceItem.TotVehs,
		&produceItem.TotMete,
		&produceItem.Qualitor,
		&produceItem.Operator,
		&produceItem.LeftTim,
		&produceItem.ArriveTim,
		&produceItem.ChkLands,
		&produceItem.ChkTemp,
		&produceItem.UnloadTim,
		&produceItem.OverTim,
		&produceItem.Acceptor,
		&produceItem.Mark,
		&produceItem.MISID,
		&produceItem.Stamp,
		&produceItem.Task,
		&produceItem.Contacts,
		&produceItem.ContTel,
		&produceItem.Bend,
	}
	if err = row.Scan(scanFields...); err != nil {
		log.Println("GetOneById scan==>>", err)
		return nil, err
	}

	return &produceItem, nil
}
